package com.ld.xuanKe.service.impl;

import com.ld.xuanKe.dao.CourseDAO;
import com.ld.xuanKe.dao.SelectdDAO;
import com.ld.xuanKe.entity.Course;
import com.ld.xuanKe.entity.MySelect;
import com.ld.xuanKe.entity.Selectd;
import com.ld.xuanKe.service.CourseService;
import com.ld.xuanKe.service.SelectdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName xuanKe
 * @ClassName SelectServiceImpl
 * @Author 刘邓
 * @Date 2023-12-09 10:42
 */
@Service
public class SelectdServiceImpl implements SelectdService {
	@Resource
	private SelectdDAO selectdDAO;
	@Resource
	private CourseService courseService;

	public int add(Selectd selectd) {

			Course course = courseService.querySingle(selectd.getCourseId());
			List<MySelect> mySelects = showMyCourse(selectd.getUserId());
			float credit = 0;
			for (MySelect mySelect : mySelects) {
				if(mySelect.getCourseName().equals(course.getName())){
					return -2;
				}
//				算该学生的总学分
				Course course1 = courseService.querySingle(selectd.getCourseId());
				credit += course1.getXf();
			}
			if(credit + course.getXf() > 35){
				return -3;
			}
			course.setSelected(course.getSelected() + 1);
			courseService.update(course);
			int i = selectdDAO.insert(selectd);
			return i;
	}

	public int delete(Integer id) {
		try {
			Selectd selectd = selectdDAO.findById(Selectd.class, id);
			Course course = courseService.querySingle(selectd.getCourseId());
			course.setSelected(course.getSelected() - 1);
			courseService.update(course);
			selectdDAO.delete(Selectd.class,id);
		} catch (Exception e) {
			return 0;
		}
		return 1;

	}

	public List<Selectd> listByUserId(Integer userId) {
	  return selectdDAO.find("from Selectd S where S.userId = ?", new Object[]{userId});
	}

	public List<MySelect> showMyCourse(Integer userId) {
		List<Selectd> selectds = selectdDAO.find("from Selectd S where S.userId = ?", new Object[]{userId});
		List<MySelect> mySelects = new ArrayList<MySelect>();
		for (Selectd selectd : selectds) {
			MySelect mySelect = new MySelect();
			mySelect.setId(selectd.getId());
			mySelect.setTime(selectd.getTime());
			Course course = courseService.querySingle(selectd.getCourseId());
			mySelect.setCourseName(course.getName());
			mySelect.setTeacherName(course.getTeacher());
			mySelect.setBz(course.getBz());
			mySelect.setXf(course.getXf());
			mySelects.add(mySelect);
		}
		return mySelects;
	}

	public List<MySelect> showMyCourseLikeName(Integer userId,String courseName) {
		List<MySelect> mySelects = showMyCourse(userId);
		List<MySelect> selects = new ArrayList<MySelect>();
		for (MySelect mySelect : mySelects) {
			if(mySelect.getCourseName().indexOf(courseName) != -1){
				selects.add(mySelect);
			}
		}
		return selects;

	}

	public List<Selectd> list() {
		return selectdDAO.find("from Selectd",new Object[]{});
	}

	public List<Selectd> listByCourseId(Integer courseId) {
		return selectdDAO.find("from Selectd where courseId = ?",new Object[]{courseId});
	}
	public List<Selectd> getValidSelectListAfterSelect(){
		List<Selectd> selectds = new ArrayList<Selectd>();
		List<Course> courses = courseService.getValidCoursesAfterSelect();
		for (Course course : courses) {
			List<Selectd> selectds1 = this.listByCourseId(course.getId());
			selectds.addAll(selectds1);
		}
		return selectds;
	}

	public float getMyCredit(Integer userId) {
		float credit = 0;
		List<Selectd> selectds = selectdDAO.find("from Selectd S where S.userId = ?", new Object[]{userId});
		for (Selectd selectd : selectds) {
			Course course = courseService.querySingle(selectd.getCourseId());
			credit += course.getXf();
		}
		return credit;
	}
}
